#define _CRT_SECURE_NO_WARNINGS 1

#include "Date.h"

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}

void Date::Print()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}

bool Date::operator<(const Date& d)
{
	if (_year < d._year) {
		return true;
	}
	else if (_year == d._year) {
		if (_month < d._month) {
			return true;
		}
		else if (_month = d._month) {
			return _day < d._day;
		}
	}
	return false;
}
bool Date::operator<=(const Date& d)
{
	if (_year > d._year) {
		return false;
	}
	else if (_year == d._year) {
		if (_month > d._month) {
			return false;
		}
		else if (_month = d._month) {
			return _day <= d._day;
		}
	}
	return true;

	// return *this < d || *this == d;
}
bool Date::operator>(const Date& d)
{
	if (_year > d._year) {
		return true;
	}
	else if (_year == d._year) {
		if (_month > d._month) {
			return true;
		}
		else if (_month = d._month) {
			return _day > d._day;
		}
	}
	return false;
}
bool Date::operator>=(const Date& d)
{
	if (_year < d._year) {
		return false;
	}
	else if (_year == d._year) {
		if (_month < d._month) {
			return false;
		}
		else if (_month = d._month) {
			return _day >= d._day;
		}
	}
	return true;
}

bool Date::operator==(const Date& d)
{
	return this->_year == d._year
		&& this->_month == d._month
		&& this->_day == d._day;
}

bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}

Date& Date::operator+=(int day)
{
	if (day < 0) {
		return *this -= -day;
	}
	_day += day;

	while (_day > GetMonthDay(_year, _month)) {
		_day -= GetMonthDay(_year, _month);
		_month++;

		if (_month > 12) {
			_month = 1;
			_year++;
		}
	}

	return *this;
}

Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp += day; // 复用

	return tmp;
}

Date& Date::operator-=(int day)
{
	if (day < 0) {
		return *this += -day;
	}

	while (_day <= day) {
		if (_month == 1) {
			_month = 12;
			_year -= 1;
		}
		else _month -= 1;
		day -= GetMonthDay(_year, _month);
	}
	_day -= day;

	return *this;
}

Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;

	return tmp;
}

int Date::operator-(const Date& d)
{
	Date greD = (*this) > d ? *this : d;
	Date smlD = (*this) < d ? *this : d;

	while (greD._year > smlD._year) {
		if (isLeapYear(greD._year - 1)) greD._day += 366;
		else greD._day += 365;
		
		greD._year -= 1;
	}

	while (greD._month > smlD._month) {
		greD._month -= 1;
		greD._day += GetMonthDay(greD._year, greD._month);
	}

	while (greD._month < smlD._month) {
		greD._month += 1;
		greD._day -= GetMonthDay(greD._year, greD._month);
	}
	int ret = greD._day - smlD._day;

	return *this > d ? ret : -ret;
}

Date& Date::operator++() 
{
	*this += 1;
	return *this;
}

Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;

	return tmp;
}

Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;

	return tmp;
}

